home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1997: The Complete Utilities Toolkit / macworld-complete-utilities-1997.iso / Programming / Little Smalltalk v3.1.5 / Smalltalk Source / basic.st next >
Encoding:
Text File  |  1994-12-02  |  8.0 KB  |  426 lines  |  [TEXT/KAHL]

  1. *
  2. * Little Smalltalk, version 3
  3. * basic methods needed for execution, including
  4. *    object creation
  5. *    block creation, execution and return
  6. *
  7. * v3.1.0    Initial release
  8. * v3.1.1    -
  9. * v3.1.2    - 
  10. * v3.1.3    - Message 'become:' added to class 'Object'.
  11. *
  12. * ***
  13. Class Object
  14. Class Block Object context argCount argLoc bytePointer
  15. Class Boolean Object
  16. Class    True Boolean
  17. Class    False Boolean
  18. Class Class Object name instanceSize methods superClass variables
  19. Class Context Object linkLocation method arguments temporaries
  20. Class Integer Object
  21. Class Method Object text message bytecodes literals stackSize temporarySize class watch
  22. Class Smalltalk Object
  23. Class Switch Object const notdone
  24. Class Symbol Object
  25. Class UndefinedObject Object
  26. *
  27. Methods Block 'initialization'
  28.     checkArgumentCount: count
  29.         ^ (argCount = count)
  30.             ifTrue: [ true ]
  31.             ifFalse: [ smalltalk error:
  32.                 'wrong number of arguments passed to block'.
  33.                 false ]
  34. |
  35.     blockContext: ctx
  36.         context <- ctx
  37. |
  38.     value
  39.         ^ (self checkArgumentCount: 0)
  40.             ifTrue: [ context returnToBlock: bytePointer ]
  41. |
  42.     value: x
  43.         ^ (self checkArgumentCount:  1)
  44.             ifTrue: [ context at: argLoc put: x.
  45.                   context returnToBlock: bytePointer ]
  46. |
  47.     value: x value: y
  48.         ^ (self checkArgumentCount: 2)
  49.             ifTrue: [ context at: argLoc put: x.
  50.                   context at: argLoc + 1 put: y.
  51.                   context returnToBlock: bytePointer ]
  52. |
  53.     value: x value: y value: z
  54.         ^ (self checkArgumentCount:  3)
  55.             ifTrue: [ context at: argLoc put: x.
  56.                   context at: argLoc + 1 put: y.
  57.                   context at: argLoc + 2 put: z.
  58.                   context returnToBlock: bytePointer ]
  59. |
  60.     whileTrue: aBlock
  61.         ( self value ) ifTrue:
  62.             [ aBlock value. 
  63.                 self whileTrue: aBlock ]
  64. |
  65.     whileTrue
  66.         self whileTrue: []
  67. |
  68.     whileFalse: aBlock
  69.         [ self value not ] whileTrue: aBlock
  70. ]
  71. Methods Boolean 'all'
  72.     ifTrue: trueBlock
  73.         ^ self ifTrue: trueBlock ifFalse: []
  74. |
  75.     ifFalse: falseBlock
  76.         ^ self ifTrue: [] ifFalse: falseBlock
  77. |
  78.     ifFalse: falseBlock ifTrue: trueBlock
  79.         ^ self ifTrue: trueBlock
  80.             ifFalse: falseBlock
  81. |
  82.     and: aBlock
  83.         ^ self ifTrue: aBlock ifFalse: [ false ]
  84. |
  85.     or: aBlock
  86.         ^ self ifTrue: [ true ] ifFalse: aBlock
  87. ]
  88. Methods Class 'creation'
  89.     new        | newObject |
  90.         newObject <- self new: instanceSize.
  91.         ^ (self == Class)
  92.             ifTrue: [ newObject initialize ]
  93.             ifFalse: [ newObject new ]
  94. |
  95.     new: size    " hack out block the right size and class "
  96.         "create a new block, set its class"
  97.         ^ < 22 < 58 size > self >
  98. |
  99.     addSubClass: aSymbol instanceVariableNames: aString    | newClass |
  100.         newClass <- Class new; name: aSymbol; superClass: self;
  101.                 variables: 
  102.                   (aString words: [:x | x isAlphabetic ]).
  103.         aSymbol assign: newClass.
  104.         classes at: aSymbol put: newClass
  105. |
  106.     initialize
  107.         superClass <- Object.
  108.         instanceSize <- 0.
  109.         methods <- Dictionary new
  110. |
  111.     methods
  112.         ^ methods
  113. |
  114.     methodNamed: name
  115.         (methods includesKey: name)
  116.             ifTrue: [ ^ methods at: name ].
  117.         (superClass notNil)
  118.             ifTrue: [ ^ superClass methodNamed: name ].
  119.         ^ nil
  120. |
  121.     name
  122.         ^ name
  123. |
  124.     name: aString
  125.         name <- aString
  126. |
  127.     instanceSize
  128.         ^ instanceSize
  129. |
  130.     printString
  131.         ^ name asString
  132. |
  133.     respondsTo    | theSet |
  134.         theSet <- Dictionary new.
  135.         self upSuperclassChain: 
  136.             [:x | theSet addAll: x methods ].
  137.         ^ theSet
  138. |
  139.     subClasses
  140.         ^ classes inject: List new
  141.             into: [:x :y | (y superClass == self)
  142.                         ifTrue: [ x add: y]. x ]
  143. |
  144.     superClass
  145.         ^ superClass
  146. |
  147.     superClass: aClass
  148.         superClass <- aClass
  149. |
  150.     upSuperclassChain: aBlock
  151.         aBlock value: self.
  152.         (superClass notNil)
  153.             ifTrue: [ superClass upSuperclassChain: aBlock ]
  154. |
  155.     variables
  156.         ^ variables
  157. |
  158.     variables: nameArray
  159.         variables <- nameArray.
  160.         instanceSize <- superClass instanceSize + nameArray size
  161. |
  162.     watch: name    | m |
  163.         m <- self methodNamed: name.
  164.         (m notNil) 
  165.             ifTrue: [ ^ m watch: 
  166.                 [:a | ('executing ', name) print. a print] ]
  167.             ifFalse: [ ^ 'no such method' ]
  168. ]
  169. Methods Context 'all'
  170.     at: key put: value
  171.         temporaries at: key put: value
  172. |
  173.     method: m
  174.         method <- m
  175. |
  176.     arguments: a
  177.         arguments <- a
  178. |
  179.     temporaries: t
  180.         temporaries <- t
  181. |
  182.     returnToBlock: bytePtr
  183.         " change the location we will return to, to execute a block"
  184.         <28 self bytePtr>
  185. |
  186.     copy
  187.         ^ super copy temporaries: temporaries copy
  188. |
  189.     blockReturn
  190.         <18 self>
  191.             ifFalse: [ ^ smalltalk error: 
  192.                 'incorrect context for block return']
  193. ]
  194. Methods False 'all'
  195.     ifTrue: trueBlock ifFalse: falseBlock
  196.         ^ falseBlock value
  197. |
  198.     not
  199.         ^ true
  200. |
  201.     xor: aBoolean
  202.         ^ aBoolean
  203. |
  204.     printString
  205.         ^ 'false'
  206. ]
  207. Methods Method 'all'
  208.     compileWithClass: aClass
  209.         ^ <39 aClass text self>
  210. |
  211.     name
  212.         ^ message
  213. |
  214.     message: aSymbol
  215.         message <- aSymbol
  216. |
  217.     printString
  218.         ^ message asString
  219. |
  220.     signature
  221.         ^ class asString,' ', message asString
  222. |
  223.     text
  224.         ^ (text notNil)
  225.             ifTrue: [ text ]
  226.             ifFalse: [ 'text not saved']
  227. |
  228.     text: aString
  229.         text <- aString
  230. |
  231.     display
  232.         ('Method ', message) print.
  233.         'text' print.
  234.         text print.
  235.         'literals' print.
  236.         literals print.
  237.         'bytecodes' print.
  238.         bytecodes class print.
  239.         bytecodes do: [:x |
  240.             (x printString, ' ', (x quo: 16), ' ', (x rem: 16))
  241.                 print ]
  242. |
  243.     executeWith: arguments
  244.         ^ ( Context new ; method: self ; 
  245.             temporaries: ( Array new: temporarySize) ;
  246.             arguments: arguments )
  247.            returnToBlock: 1
  248. |
  249.     watch: aBlock
  250.         watch <- aBlock
  251. |
  252.     watchWith: arguments
  253.         " note that we are being watched "
  254.         text print.
  255.         watch value: arguments.
  256.         ^ self executeWith: arguments
  257. ]
  258. Methods Object 'all'
  259.     assign: name value: val
  260.         ^ name assign: val
  261. |
  262.     == aValue
  263.         ^ <21 self aValue>
  264. |
  265.     ~~ aValue
  266.         ^ (self == aValue) not
  267. |
  268.     = aValue
  269.         ^ self == aValue
  270. |
  271.     asString
  272.         ^ self printString
  273. |
  274.     basicAt: index
  275.         ^ <25 self index>
  276. |
  277.     basicAt: index put: value
  278.         ^ <31 self index value>
  279. |
  280.     basicSize
  281.         ^ <12 self>
  282. |
  283.     class
  284.         ^ <11 self>
  285. |
  286.     copy
  287.         ^ self shallowCopy
  288. |
  289.     deepCopy    | newObj |
  290.         newObj <- self class new.
  291.         (1 to: self basicSize) do: 
  292.             [:i | newObj basicAt: i put: (self basicAt: i) copy].
  293.         ^ newObj
  294. |
  295.     display
  296.         ('(Class ', self class, ') ' , self printString ) print
  297. |
  298.     hash
  299.         ^ <13 self>
  300. |
  301.     isMemberOf: aClass
  302.         ^ self class == aClass
  303. |
  304.     isNil
  305.         ^ false
  306. |
  307.     isKindOf: aClass
  308.         self class upSuperclassChain:
  309.             [:x | (x == aClass) ifTrue: [ ^ true ] ].
  310.         ^ false
  311. |
  312.     new
  313.         " default initialization protocol"
  314.         ^ self
  315. |
  316.     notNil
  317.         ^ true
  318. |
  319.     print
  320.         self printString print 
  321. |
  322.     printString
  323.         ^ self class printString
  324. |
  325.     respondsTo: message
  326.         self class upSuperclassChain: 
  327.             [:c | (c methodNamed: message) notNil
  328.                     ifTrue: [ ^ true ]].
  329.         ^ false
  330. |
  331.     shallowCopy    | newObj |
  332.         newObj <- self class new.
  333.         (1 to: self basicSize) do: 
  334.             [:i | newObj basicAt: i put: (self basicAt: i) ].
  335.         ^ newObj
  336. |
  337.     " Following method added by Julian Barkway for v3.1.3 "  
  338.     become: otherObject
  339.         ^ <20 self otherObject>
  340. ]
  341. Methods Smalltalk 'all'
  342.     perform: message withArguments: args ifError: aBlock    
  343.             | receiver method |
  344.         receiver <- args at: 1 ifAbsent: [ ^ aBlock value ].
  345.         method <- receiver class methodNamed: message.
  346.         ^ method notNil 
  347.             ifTrue: [ method executeWith: args ]
  348.             ifFalse: aBlock
  349. |
  350.     perform: message withArguments: args
  351.         ^ self perform: message withArguments: args
  352.             ifError: [ self error: 'cant perform' ]
  353. |
  354.     watch
  355.         ^ <5>
  356. ]
  357. Methods True 'all'
  358.     ifTrue: trueBlock ifFalse: falseBlock
  359.         ^ trueBlock value
  360. |
  361.     not
  362.         ^ false
  363. |
  364.     xor: aBoolean
  365.         ^ aBoolean not
  366. |
  367.     printString
  368.         ^ 'true'
  369. ]
  370. Methods Switch 'all'
  371.     key: value
  372.         const <- value.
  373.         notdone <- true.
  374. |
  375.     ifMatch: key do: block
  376.         (notdone and: [ const = key ])
  377.             ifTrue: [ notdone <- false. block value ]
  378. |
  379.     else: block
  380.         notdone ifTrue: [ notdone <- false. block value ]
  381. ]
  382. Methods Symbol 'all'
  383.         apply: args
  384.         ^ self apply: args ifError: [ 'does not apply' ]
  385. |
  386.         apply: args ifError: aBlock
  387.         ^ smalltalk perform: self withArguments: args ifError: aBlock
  388. |
  389.     assign: value
  390.         <27 self value>. ^ value
  391. |
  392.     asString
  393.         " catenation makes string and copy automatically "
  394.         ^ <24 self ''>
  395. |
  396.     copy
  397.         ^ self
  398. |
  399.     printString
  400.         ^ '#' , self asString
  401. |
  402.     respondsTo
  403.         ^ classes inject: Set new
  404.             into: [:x :y | ((y methodNamed: self) notNil)
  405.                         ifTrue: [ x add: y]. x]
  406. |
  407.     value
  408.         ^ <87 self>
  409. ]
  410. Methods UndefinedObject 'all'
  411.     isNil
  412.         ^ true
  413. |
  414.     notNil
  415.         ^ false
  416. |
  417.     printString
  418.         ^ 'nil'
  419. ]
  420. Methods Object 'errors'
  421.     message: m notRecognizedWithArguments: a
  422.         ^ smalltalk error: 'not recognized ', (self class printString),
  423.             ' ', (m printString)
  424. ]
  425.  
  426.